package cn.com.inossem.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.inossem.activity.JobsDetailsActivity;
import cn.com.inossem.activity.ListJobsActivity;
import cn.com.inossem.activity.ProjectDetailActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.fragment.JobsOpportunityFragment;
import cn.com.inossem.model.QueryProjectInfoBean;
import cn.com.inossem.utils.StringUtil;

public class ListJobsItemAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<QueryProjectInfoBean> messageInfo;
	public List<QueryProjectInfoBean> selected;
	public List<ImageView> listImage;
	public List<Integer> listFlag;
	List<QueryProjectInfoBean> Xuanzhongmessage;

	boolean flag = false;
	int type = 0;
	private Intent intent;

	public ListJobsItemAdapter(Context mContext,
	                           List<QueryProjectInfoBean> messageInfo, int type,
	                           List<QueryProjectInfoBean> Xuanzhongmessage) {
		super();
		this.mContext = mContext;
		this.messageInfo = messageInfo;
		this.Xuanzhongmessage = Xuanzhongmessage;
		mInflater = LayoutInflater.from(mContext);
		selected = new ArrayList<QueryProjectInfoBean>();
		listImage = new ArrayList<ImageView>();
		listFlag = new ArrayList<Integer>();
		for (int i = 0; i < messageInfo.size(); i++) {
			listFlag.add(0);
		}
		this.type = type;
	}

	public void setList(List<QueryProjectInfoBean> messageInfo) {
		this.messageInfo = messageInfo;
	}

	@Override
	public int getCount() {
		// return messageInfo.size();
		return messageInfo.size();
	}

	@Override
	public QueryProjectInfoBean getItem(int arg0) {
		// return messageInfo.get(arg0);
		return messageInfo.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_jobs_item, null);
			holder.llayout = (LinearLayout) convertView
					.findViewById(R.id.llayout);
			holder.jobname = (TextView) convertView.findViewById(R.id.jobname);
			holder.positions = (TextView) convertView
					.findViewById(R.id.positions);
			holder.projectIndustry = (TextView) convertView
					.findViewById(R.id.projectIndustry);
			holder.modelRequire = (TextView) convertView
					.findViewById(R.id.modelRequire);
			holder.startDate = (TextView) convertView
					.findViewById(R.id.startDate);
			holder.projectCycle = (TextView) convertView
					.findViewById(R.id.projectCycle);
			holder.projectCycle1 = (TextView) convertView
					.findViewById(R.id.projectCycle1);
			holder.projectCycle2 = (TextView) convertView
					.findViewById(R.id.projectCycle2);
			holder.projectAddress = (TextView) convertView
					.findViewById(R.id.projectAddress);
			holder.releasedate = (TextView) convertView
					.findViewById(R.id.releasedate);
			holder.zhuangtai = (TextView) convertView
					.findViewById(R.id.zhuangtai);
			holder.choose = (ImageView) convertView.findViewById(R.id.choose);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.jobname.setText(getItem(position).projectName);
		holder.positions.setText("职位：" + getItem(position).station);
		holder.projectIndustry.setText("行业：" + getItem(position).projectIndustry);
		holder.modelRequire.setText("模块：" + getItem(position).modelRequire);
		holder.startDate.setText(getItem(position).startDate);
		if (getItem(position).projectCycle.equals("-99")) {
			holder.projectCycle.setText("入职");
			holder.projectCycle1.setVisibility(View.GONE);
			holder.projectCycle2.setVisibility(View.GONE);
		} else {
			holder.projectCycle.setText(getItem(position).projectCycle);
			holder.projectCycle1.setVisibility(View.VISIBLE);
			holder.projectCycle2.setVisibility(View.VISIBLE);
		}
		holder.projectAddress.setText(getItem(position).projectAddress);

		if (type == 0) {
			if (listFlag.get(position) == 0) {
				listFlag.add(position, 1);
				listImage.add(holder.choose);
				this.notifyDataSetChanged();
			} else {
				if (flag) {
					listImage.get(position).setVisibility(View.VISIBLE);
				} else {
					listImage.get(position).setVisibility(View.GONE);
				}
				if (getItem(position).isselected == 1) {
					listImage.get(position).setBackgroundResource(
							R.drawable.xuanze);
					System.out.println("选中刷新:" + position);
				} else {
					listImage.get(position).setBackgroundResource(
							R.drawable.weixuanze);
					System.out.println("未选中刷新:" + position);
				}

				listImage.get(position).setOnClickListener(
						new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								if (getItem(position).isselected == 1) {
									getItem(position).isselected = 0;
									listImage.get(position)
											.setBackgroundResource(
													R.drawable.weixuanze);

									if (Xuanzhongmessage.contains(messageInfo
											.get(position))) {
										System.out.println("取消:" + position);
										Xuanzhongmessage.remove(messageInfo
												.get(position));
									}
								} else {
									getItem(position).isselected = 1;
									listImage.get(position)
											.setBackgroundResource(
													R.drawable.xuanze);
									if (!Xuanzhongmessage.contains(messageInfo
											.get(position))) {
										System.out.println("选中:" + position);
										Xuanzhongmessage.add(messageInfo
												.get(position));
									}
								}
							}
						});
			}
		}
		holder.releasedate.setVisibility(View.GONE);
		String statusStr = getItem(position).status;
		if (StringUtil.isNotBlank(statusStr)) {
			switch (Integer.parseInt(statusStr)) {
				case 10991:// 申请中
					holder.zhuangtai.setVisibility(View.VISIBLE);
					holder.zhuangtai.setBackgroundResource(R.drawable.shenqingzhong);
					holder.zhuangtai.setText("申请中");
					break;
				case 10992:// 已获得
					holder.zhuangtai.setVisibility(View.VISIBLE);
					holder.zhuangtai.setBackgroundResource(R.drawable.yihuode);
					holder.zhuangtai.setText("已获得");
					break;
				case 13228:// 已失效
					holder.zhuangtai.setVisibility(View.VISIBLE);
					holder.zhuangtai.setBackgroundResource(R.drawable.yishixiao);
					holder.zhuangtai.setText("已失效");
					break;
				default:
					break;
			}
		}
		holder.llayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				JobsOpportunityFragment.qpib = messageInfo.get(position);
				if (type == 1) {
					if (StringUtil.isNotBlank(statusStr)) {
						if (Integer.parseInt(statusStr) == 10992) {
							intent = new Intent(mContext, ProjectDetailActivity.class);
							intent.putExtra("id", messageInfo.get(position).id);
							mContext.startActivity(intent);
						} else {
							intent = new Intent(mContext, JobsDetailsActivity.class);
							intent.putExtra("id", messageInfo.get(position).id);
							((ListJobsActivity) mContext).startActivityForResult(intent, 99);
						}
					}
				} else {
					intent = new Intent(mContext, JobsDetailsActivity.class);
					intent.putExtra("id", messageInfo.get(position).id);
					((ListJobsActivity) mContext).startActivityForResult(intent, 99);
				}
			}
		});

		return convertView;
	}

	public void showButton() {
		flag = true;
		this.notifyDataSetChanged();
	}

	public void hiddenButton() {
		flag = false;
		this.notifyDataSetChanged();
	}

	public final class ViewHolder {
		public TextView jobname;
		public TextView positions;
		public TextView projectIndustry;
		public TextView modelRequire;
		public TextView startDate;
		public TextView projectCycle;
		public TextView projectCycle1;
		public TextView projectCycle2;
		public TextView projectAddress;
		public TextView releasedate;
		public TextView zhuangtai;
		public ImageView choose;
		public LinearLayout llayout;
	}

}
